package top.lyjwn.todo.common.utils;


import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException;
import com.alibaba.nacos.api.exception.runtime.NacosSerializationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

public final class JacksonUtil {
    static ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    private JacksonUtil() {
    }

    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException var2) {
            throw new NacosSerializationException(obj.getClass(), var2);
        }
    }

    public static byte[] toJsonBytes(Object obj) {
        try {
            return mapper.writeValueAsBytes(obj);
        } catch (JsonProcessingException var2) {
            throw new NacosSerializationException(obj.getClass(), var2);
        }
    }

    public static <T> T toObj(byte[] json, Class<T> cls) {
        try {
            return mapper.readValue(json, cls);
        } catch (Exception var3) {
            throw new NacosDeserializationException(cls, var3);
        }
    }

    public static <T> T toObj(byte[] json, Type cls) {
        try {
            return mapper.readValue(json, mapper.constructType(cls));
        } catch (Exception var3) {
            throw new NacosDeserializationException(var3);
        }
    }

    public static <T> T toObj(InputStream inputStream, Class<T> cls) {
        try {
            return mapper.readValue(inputStream, cls);
        } catch (IOException var3) {
            throw new NacosDeserializationException(var3);
        }
    }

    public static <T> T toObj(byte[] json, TypeReference<T> typeReference) {
        try {
            return mapper.readValue(json, typeReference);
        } catch (Exception var3) {
            throw new NacosDeserializationException(var3);
        }
    }

    public static <T> T toObj(String json, Class<T> cls) {
        try {
            return mapper.readValue(json, cls);
        } catch (IOException var3) {
            throw new NacosDeserializationException(cls, var3);
        }
    }

    public static <T> T toObj(String json, Type type) {
        try {
            return mapper.readValue(json, mapper.constructType(type));
        } catch (IOException var3) {
            throw new NacosDeserializationException(var3);
        }
    }

    public static <T> T toObj(String json, TypeReference<T> typeReference) {
        try {
            return mapper.readValue(json, typeReference);
        } catch (IOException var3) {
            throw new NacosDeserializationException(typeReference.getClass(), var3);
        }
    }

    public static <T> T toObj(InputStream inputStream, Type type) {
        try {
            return mapper.readValue(inputStream, mapper.constructType(type));
        } catch (IOException var3) {
            throw new NacosDeserializationException(type, var3);
        }
    }

    public static JsonNode toObj(String json) {
        try {
            return mapper.readTree(json);
        } catch (IOException var2) {
            throw new NacosDeserializationException(var2);
        }
    }
}
